<div class="row">
	<div class="col-xs-12">
		<div class="box">
			<div class="box-header">
				<h3 class="box-title">日志管理</h3>

				<div class="box-tools">
					<div class="input-group input-group-sm" style="width: 350px;">
						<input type="text" name="table_search" id="searchNameId"
							class="form-control pull-right" placeholder="用户名">

						<div class="input-group-btn">
							<button type="button" class="btn btn-default btn-search">
                                <i class="fa fa-search"></i>
							</button>
							<button type="button" class="btn btn-default btn-delete">删除</button>
						</div>
					</div>
				</div>
			</div>
			<!-- /.box-header -->
			<div class="box-body table-responsive no-padding">
				<table class="table table-hover">
					<thead>
						<tr>
						    <th><input type="checkbox" id="checkAll">全选</th>
							<th>用户名</th>
							<th>操作</th>
							<th>请求方法</th>
							<th>请求参数</th>
							<th>IP地址</th>
							<th>执行时间</th>
						</tr>
					</thead>
					<tbody id="tbodyId">
					    <tr>
					       <td colspan="7">数据加载中...</td>
					    </tr>
					</tbody>
				</table>
			</div>
			<div id="pageId" class="box-footer clearfix">
			      
			</div>
			<!-- /.box-body -->
		</div>
		<!-- /.box -->
	</div>
</div>
<script type="text/javascript">
	$(function(){
		$("#pageId").load("doPageUI",function(){
		      //异步加载日志信息(数据)
		      doGetObjects();
		});
		$(".input-group-btn").on("click",".btn-delete",doDeleteObjects);
	    $(".input-group-btn").on("click",".btn-search",doQueryObjects);
	    $("#checkAll").click(doChangeTBodyCheckBoxState);
	    $("#tbodyId").on("click",".cBox",doChangeTHeadCheckBoxState);
	})
	
	function doGetObjects(){
		var url="log/doFindPageObjects";
		var pageCurrent=$("#pageId").data("pageCurrent");
		if(!pageCurrent){
			pageCurrent=1;
		}
		var params={"pageCurrent":pageCurrent};
		var username=$("#searchNameId").val();
		if(username) params.username=username;
		$.getJSON(url,params,function(result){
			//console.log(result);
			//console.log(result.data.records);
			doHandleResponseResult(result);
		});
	}
	function doHandleResponseResult(result){
		if(result.state==1){
			doSetTableBodyRow(result.data.records);
			doSetPagination(result.data);
		}else{
			alert(result.message);
		}
	}
	function doSetTableBodyRow(records){
		var tBody=$("#tbodyId")
		tBody.empty();
		for(var i=0;i<records.length;i++){
			var tr=$("<tr></tr>");
			var tds=doCreateTds(records[i]);
			tr.append(tds);
			tBody.append(tr);
		}
	}
	function doCreateTds(data){
		 var tds="<td><input type='checkbox' class='cBox' name='cItem' value='"+data.id+"'></td>"+
	   	 "<td>"+data.username+"</td>"+
	     "<td>"+data.operation+"</td>"+
	     "<td>"+data.method+"</td>"+
	     "<td>"+data.params+"</td>"+
	     "<td>"+data.ip+"</td>"+
	     "<td>"+data.time+"</td>";	   
		return tds;
	}
	function doQueryObjects(){
		$("#pageId").data("pageCurrent",1);
		doGetObjects();
	}
	function doDeleteObjects(){
		var idArray=doGetCheckedIds();
		if(idArray.length==0){
			alert("至少选择一个");
			return;
		}
		if(!confirm("确认删除吗")){
			return;
		}
		var url="log/doDeleteObjects";
		var params={"ids":idArray.toString()};
		$.post(url,params,function(result){
			if(result.state==1){
				alert(result.message);
				doGetObjects();
			}else{
				alert(result.message);
			}
		});
	}
	function doGetCheckedIds(){
		var array=[];
		$("#tbodyId input[type='checkbox']")
		.each(function(){
			if($(this).prop("checked")){
				array.push($(this).val())
			}
		});
		return array;
	}
	function doChangeTBodyCheckBoxState(){
		var flag = $(this).prop("checked");
		$("#tbodyId input[type='checkbox']").prop("checked", flag);
	}
	function doChangeTHeadCheckBoxState(){
		var len1=$("#tbodyId input[type='checkbox']").length;
		var len2=$("#tbodyId input[type='checkbox'] [checked]").length;
		$("#checkAll").prop("checked", len1==len2);
	}
</script>
